package com.hq.hotl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hq.hotl.dao.UserDao;
import com.hq.hotl.pojo.Customer;
import com.hq.hotl.pojo.House;
import com.hq.hotl.pojo.PageData;
import com.hq.hotl.pojo.User;
import com.hq.hotl.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    final public static int DEFAULT_PAGE_NUMBER=1;
    final public static int DEFAULT_PAGE_SIZE=2;

    @Autowired
    private UserDao userDao;

    public UserServiceImpl() {
    }

    public House orderHouse(Customer customer) {
//        House house = userDao.orderHouse(customer);
//        return house;
        return null;
    }

    @Override
    public User getOne(String username) {
        User user = userDao.queryOne(username);
        return user;
    }

    @Override
    public User userlogin(String username, String password) {
        User login = userDao.userlogin(username, password);

        /*Subject subject= SecurityUtils.getSubject();
        if (!subject.isAuthenticated()){
            AuthenticationToken token=new UsernamePasswordToken(username,password);
            try {
                subject.login(token);
            }catch (UnknownAccountException e){
                return login;
            }catch (IncorrectCredentialsException e){
                return login;
            }catch (AuthenticationException e){
                return login;
            }
        }
        //已经认证过
        //从shiro提供session对象中获取已经认证成功的用户信息
        Object user= subject.getSession().getAttribute("user");*/
        return login;
    }

    @Override
    public void updatauser(User user) {
        userDao.updateOne(user);
    }

    @Override
    public PageData getAllByPage(int currentPage, int pageSize) {
        //判断入参的合理性
        if (currentPage<DEFAULT_PAGE_NUMBER){
            currentPage=DEFAULT_PAGE_NUMBER;
        }
        if (pageSize<DEFAULT_PAGE_SIZE){
            pageSize=DEFAULT_PAGE_SIZE;
        }
        //利用pagehelper插件开始分页，拦截下一次的mybatis的查询操作
        PageHelper.startPage(currentPage,pageSize);
        //查询全部用户信息
        List<User> userList=userDao.queryAll();
        //pageInfo对象已经计算好分页相关信息了
        PageInfo<User> PageInfo = new PageInfo<>(userList);
        //利用pageInfo对象来封装我们自定义的PageData
        PageData<User> pageData = new PageData<>();
        // 设置总页数
        pageData.setTotalPages(PageInfo.getPages());
        //设置总记录数
        pageData.setTotalSize(PageInfo.getTotal());
        //设置上一页
        if (PageInfo.isHasPreviousPage()){
            //有上一页，即没在首页
            pageData.setPreviousPage(PageInfo.getPrePage());
        }else {
            //当前页码在首页
            pageData.setPreviousPage(DEFAULT_PAGE_NUMBER);
        }
        //设置下一页页码
        if (PageInfo.isHasNextPage()){
            //当前页码没在尾页
            pageData.setNextPage(PageInfo.getNextPage());
        }else {
            //当前页码在尾页
            //下一页页码设置为尾页
            pageData.setNextPage(PageInfo.getPages());
        }
        //设置当前页码和每页记录路数
        pageData.setCurrentPage(currentPage);
        pageData.setPageSize(pageSize);
        //设置查询当前的数据
        pageData.setData(PageInfo.getList());
        //返回数据模型
        return pageData;
    }

    @Override
    public Map<String, Object> logout() {
        //获取当前用户
        Subject subject=SecurityUtils.getSubject();
        //执行登出，删除相关的登录信息
        subject.logout();
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","用户登出成功");
        return result;
    }

    @Override
    public void deleteuser(int id) {
        userDao.deleteOne(id);
    }

}
